package com.kly.juc.demo;

import lombok.Data;

/**
 * @author admin
 * @date 2025/4/19
 */
public class MovieDemo {
    public static void main(String[] args) {
        sellTicket();
    }

    /**
     * 卖票
     */
    public static void sellTicket() {
    //     自定义票数
        int num = 100;
        Ticket ticket = new Ticket();
        ticket.setNum(num);
        ticket.setName("电影票");

        for (int i = 0; i < 102; i++) {
            new Thread(() -> {
                if (ticket.getNum() <= 0) {
                    System.out.println("票卖完了");
                    return;
                }
                ticket.sell();
                System.out.println(ticket.getName() + "剩余：" + ticket.getNum());
            }).start();

            new Thread(() -> {
                if (ticket.getNum() <= 0) {
                    System.out.println("票卖完了");
                    return;
                }
                ticket.sell();
                System.out.println(ticket.getName() + "剩余：" + ticket.getNum());
            }).start();
        }

        System.out.println("ticket = " + ticket.getNum());


    }


}

@Data
class Ticket {
    private int num;
    private String name;

    public void sell() {
        num--;
    }
}
